<?php

global $wp_version;
if ( version_compare($wp_version, '2.5', '<') ) {
	update_option('template', 'default');
	update_option('stylesheet', 'default');
	delete_option('current_theme');
	// Flush the dang cache
	wp_cache_flush();

	wp_die('Dkret requires WordPress 2.5 or higher. Reverted to default theme. Please reload this page.');
}

// Version and path to check version

$options = get_option('dkret3');
$checkscheme = @opendir(STYLESHEETPATH . "/schemes/");
if ($checkscheme && $options['scheme'] != 'standard')
	$scheme = get_bloginfo('stylesheet_directory') . '/schemes/' . $options['scheme'];
else
	$scheme = 'standard';

$themeData = get_theme_data(TEMPLATEPATH . '/style.css');
$installedVersion = trim($themeData['Version']);
if(!$installedVersion) {
	$installedVersion = "unknown";
}
define('DKRETUPDATE', "http://diekretzschmars.de/dkretversion.php");
define('DKRETSCHEME', $scheme);
define('THEMENAME', $themeData['Title']);
define('THEMEAUTHOR', $themeData['Author']);
define('THEMEURI', $themeData['URI']);
define('DKRETVERSION', $installedVersion);

$cur=(STYLESHEETPATH . '/style.css');
$localhost=strstr($cur, 'localhost');
$ct=get_theme_data($cur);

$installedVersion = trim($ct['Version']);
if(!$installedVersion) {
	$installedVersion = "unknown";
}

define('TEMPLATENAME', $ct['Title']);
define('TEMPLATEAUTHOR', $ct['Author']);
define('TEMPLATEURI', $ct['URI']);
define('TEMPLATEVERSION', $installedVersion);

// import integrated Plugins
require(ABSPATH . '/wp-content/themes/' . get_template() .'/functions-dkret.php');

// localisation won't work without
$locale = get_locale();
if ( empty($locale) )
	$locale = 'en_US';

$checklanguage = @opendir(STYLESHEETPATH . "/languages/");
if ($checklanguage)
	$mofile = STYLESHEETPATH . "/languages/dkret-$locale.mo";
else
	$mofile = TEMPLATEPATH . "/languages/dkret-$locale.mo";

load_textdomain('dkret', $mofile);
	
// add javascript to header
add_action('wp_head', 'dkret_addjs', 1);

// Add dkret widgets and properly unregister standard widgets
add_action( 'init', 'dkret_widgets_init' );

// delete  all options if theme is switched
add_action('switch_theme', 'dkret_switch');

function dkret_addjs() {
	wp_enqueue_script('jquery');
	wp_enqueue_script('jquery-comments', get_bloginfo('template_url') .'/ajax/dkret.js.php', array('jquery'), '3.0');
	if (is_file(STYLESHEETPATH . "/functions.js")) wp_enqueue_script('functions', '/wp-content/themes/' .get_stylesheet() . '/functions.js', array('jquery'));
}

function dkret_widgets_init() {
	if ( !function_exists('register_sidebars') )
		return;

		// WIDGETS
		register_sidebar(array(
			'name'=>'Primary Sidebar',
			'before_title' => "<h3 class='widgettitle'>",
			'after_title' => "</h3>\n",
		));

		register_sidebar(array(
			'name'=>'Sidebar Column1',
			'before_title' => "<h3 class='widgettitle'>",
			'after_title' => "</h3>\n",
		));
	  
		register_sidebar(array(
			'name'=>'Sidebar Column2',
			'before_title' => "<h3 class='widgettitle'>",
			'after_title' => "</h3>\n",
		));

		register_sidebar(array(
			'name'=>'Secondary Sidebar',
			'before_title' => "<h3 class='widgettitle'>",
			'after_title' => "</h3>\n",
		));

		register_sidebar(array(
			'name'=>'Bottombar Column1',
			'before_title' => "<h3 class='widgettitle'>",
			'after_title' => "</h3>\n",
		));

		register_sidebar(array(
			'name'=>'Bottombar Column2',
			'before_title' => "<h3 class='widgettitle'>",
			'after_title' => "</h3>\n",
		));

		register_sidebar(array(
			'name'=>'Bottombar Column3',
			'before_title' => "<h3 class='widgettitle'>",
			'after_title' => "</h3>\n",
		));

		// now have a look for all available additional widgets and activate them
		$widgets_dir = @ dir(ABSPATH . '/wp-content/themes/' . get_template() . '/widgets');
		if ($widgets_dir)	{
			while(($widgetFile = $widgets_dir->read()) !== false) {
				if (!preg_match('|^\.+$|', $widgetFile) && preg_match('|\.php$|', $widgetFile))
					include(ABSPATH . '/wp-content/themes/' . get_template() . '/widgets/' . $widgetFile);
			}
		}

		// now have a look for all available additional widgets in the template theme  and activate them
		$widgets_dir = @ dir(ABSPATH . '/wp-content/themes/' . get_stylesheet() . '/widgets');
		if ((TEMPLATENAME != THEMENAME) && ($widgets_dir)) {
			while(($widgetFile = $widgets_dir->read()) !== false) {
				if (!preg_match('|^\.+$|', $widgetFile) && preg_match('|\.php$|', $widgetFile))
					include(ABSPATH . '/wp-content/themes/' . get_stylesheet() . '/widgets/' . $widgetFile);
			}
		}
}

//save changed options
function dkretupdate() {
	if ( !empty($_POST) ) {
		$options = get_option('dkret3');
		if ( isset($_POST['scheme_file']) )
			$options['scheme'] = $_POST['scheme_file'];
		if ( isset($_POST['header_text']) )
			$options['blogtitle'] = '1';
		else
			$options['blogtitle'] = '0';
		if ( isset($_POST['header_rss']) )
			$options['rss'] = '1';
		else
			$options['rss'] = '0';
		if ( isset($_POST['show_subnavigation']) )
			$options['navigationlevel'] = $_POST['show_subnavigation'];
		if ( isset($_POST['nowreading']) )
			$options['nowreading'] = '1';
		else
			$options['nowreading'] = '0';
		if ( isset($_POST['textalign']) )
			$options['textalign'] = '1';
		else
			$options['textalign'] = '0';
 		if ( isset($_POST['imageborders']) )
			$options['imageborders'] = '1';
		else
			$options['imageborders'] = '0';
 		if ( isset($_POST['summary']) )
			$options['summary'] = '1';
		else
			$options['summary'] = '0';
		if ( isset($_POST['livecomments']) )
			$options['livecomments'] = '1';
		else
			$options['livecomments'] = '0';
		if ( isset($_POST['previewcomment']) )
			$options['previewcomment'] = '1';
		else
			$options['previewcomment'] = '0';
		if ( isset($_POST['commentsorder']) )
			$options['commentsorder'] = $_POST['commentsorder'];
		if ( isset($_POST['cat']) )
			$options['gallery_cat_id'] = $_POST['cat'];
		if ( isset($_POST['gallerylink']) )
			$options['gallerylink'] = '1';
		else
			$options['gallerylink'] = '0';
		if ( isset($_POST['showgallery']) )
			$options['showgallery'] = '1';
		else
			$options['showgallery'] = '0';
		if ( isset($_POST['userandomfeature']) )
			$options['random_header'] = '1';
		else
			$options['random_header'] = '0';
		if ( isset($_POST['header_picture']) )
			$options['headerimage'] = $_POST['header_picture'];
		if ($_FILES['picture']['name'] != "" && $_FILES['picture']['size'] > 0) {
			$target_path =  ABSPATH . '/wp-content/themes/' . get_template() . '/images/headers/';
			move_uploaded_file($_FILES['picture']['tmp_name'], $target_path.$_FILES['picture']['name']);
			$options['headerimage'] = ($_FILES['picture']['name']);
		}
		update_option('dkret3', $options);
	}
}

// if we can't find dkret3 installed or installed version is older lets go ahead and install all the options that run dkret3.
$options = get_option('dkret3');
if ( (!$options) || ($options['version'] != DKRETVERSION) ) {
	dkret_switch();
	$options = array(
		'version'=>DKRETVERSION,
		'scheme'=>'standard',
		'blogtitle'=>'1',
		'rss'=>'0',
		'navigationlevel'=>'1',
		'nowreading'=>'1',
		'textalign'=>'1',
		'imageborders'=>'0',
		'summary'=>'0',
		'livecomments'=>'1',
		'previewcomment'=>'1',
		'commentsorder'=>'1',
		'random_header'=>'0',
		'headerimage'=>'standard',
		'gallery_cat_id'=>'-1',
		'gallerylink'=>'1',
		'showgallery'=>'0',
		);
	update_option('dkret3', $options);
}

function dkret_switch() {
	delete_option('dkret3');

	// delete options of the dkret Widgets
	// delete_option('widget_dkret_about');
	delete_option('widget_dkret_links');
	delete_option('widget_dkret_meta');
	delete_option('widget_dkret_randomquote');
	delete_option('widget_dkret_search');
	delete_option('widget_dkret_tagcloud');
	delete_option('widget_dkret_todo');
	delete_option('widget_dkret_icalevents');
	delete_option('widget_dkret_events');
	delete_option('widget_dkret_rsslinks');
	delete_option('widget_dkret_nowreading');
}

// Let's add the options page.
add_action ('admin_menu', 'dkretmenu');

$dkretloc = '../themes/' . basename(dirname($file)); 

function dkretmenu() {
	add_submenu_page('themes.php', 'dkret Options', TEMPLATENAME . ' ' .__('Options','dkret') , 5, $dkretloc . 'functions.php', 'menu');
}

function menu() {
	global $wp_version;
	$options=get_option('dkret3');
	
	if (isset($_POST['Submit'])) {
?>
	<div id="mesage" class="updated fade">
		<p><strong><?php _e('Settings saved','dkret'); ?></strong></p>
	</div>
<?php
	}
?>

<?php
	if (!exist_widget_column('all')) {
?>
	<p class="updated fade"><strong><a href='<?php bloginfo('url'); ?>/wp-admin/widgets.php'><?php _e("Please add some Widgets!",'dkret'); ?></a></strong></p>
<?php
	}
?>

	<div class="wrap">
		<h2><?php echo TEMPLATENAME; echo ' '; _e('Options','dkret'); ?></h2>

			<p>
				<span style="float: right;"><?php printf(__('Version: <strong>%s</strong>', 'dkret'), TEMPLATEVERSION) ?></span>
<?php
				printf(__('Developed by %s ', 'dkret'), TEMPLATEAUTHOR);
				if (THEMENAME != TEMPLATENAME) {
					printf(__('(based on <a href="%1$s">%2$s</a>)', 'dkret'), THEMEURI, THEMENAME);
				}
				printf(__('<br />Download new versions at the <a href="%1$s">%2$s</a> homepage', 'dkret'), TEMPLATEURI, TEMPLATENAME)
?>
			</p>

			<form name="dofollow" action="" method="post" enctype="multipart/form-data">
				<input type="hidden" name="action" value="<?php dkretupdate(); ?>" />
				<input type="hidden" name="page_options" value="'dofollow_timeout'" />

<?php
	$options=get_option('dkret3');
	global $checkscheme;
	if ($checkscheme) {
?>
				<h3><?php _e('Schemes','dkret'); ?></h3>
				<p><?php _e("Schemes are CSS files that allow you to visually customize your blog, without ever touching the themes core files.<br />Read the documentation for further information.",'dkret'); ?></p>

				<table class="form-table">
					<tbody>
						<tr valign="top">
							<th scope="row"><?php _e('Schemes','dkret'); ?></th>
							<td>
								<select name="scheme_file" id="scheme_file" style="width: 200px;">
								<option value="<?php echo $options['scheme'] ?>"><?php if ($options['scheme'] == 'standard') { _e('Standard Scheme','dkret'); } else { echo $options['scheme']; } ?></option>
								<option value=""> ------ </option>
								<option value="standard"><?php _e('Use Standard Design','dkret') ?></option>
    
<?php
	$scheme_dir = @ dir(ABSPATH . '/wp-content/themes/' . get_option('stylesheet') . '/schemes');

	if ($scheme_dir) {
		while(($file = $scheme_dir->read()) !== false) {
			if (!preg_match('|^\.+$|', $file) && preg_match('|\.css$|', $file)) 
				$scheme_files[] = $file;
			}
		}
		if ($scheme_dir || $scheme_files) {
			foreach($scheme_files as $scheme_file) {
				echo '<option value="' . $scheme_file . '">' . $scheme_file . '</option>';
			}
		} 
		echo '</select>'; ?>
							</td>
						</tr>
					</tbody>
				</table>

<?php 
	}
?>

				<h3><?php _e('Header','dkret'); ?></h3>
				<p><?php _e("Choose the picture you would like to upload to the server and you are done.<br />You can even upload multiple files and randomly circle through them if you enable <strong>'Show Random Header Picture.'</strong>",'dkret'); ?></p>

				<script type="text/javascript">
					var blogadress = "<?php echo get_template(); ?>";
					function updateHeader(sel) {
						if (sel.options[sel.selectedIndex].value == "standard") {
							document.header.src = "../wp-content/themes/" + blogadress + "/images/bg_header_standard.jpg";
						}	else {
							document.header.src = "../wp-content/themes/" + blogadress + "/images/headers/" + sel.options[sel.selectedIndex].value;
						}
					}
				</script>

				<table class="form-table">
					<tbody>
						<tr valign="top">
							<th scope="row"><?php _e('Upload Images','dkret'); ?></th>
							<td>
								<label for="picture">
<?php
	$picture_dir = ABSPATH . '/wp-content/themes/' . get_template() . '/images/headers';
	// check for permission settings, Safe mode limitations are not taken into account. 
	if ( !is_writeable($picture_dir ) ) {
		$picture_dir = '/' . get_template() . '/images/headers';
		printf(__('Folder <strong>%1$s</strong> is not writable. Upload wont work.', 'dkret'), $picture_dir);
	} else {
?>
									<input id="picture" size="30" type="file" name="picture" /><br />
									<?php _e("Press <strong>'Save changes'</strong> at the bottom to upload the image.",'dkret') ?>
<?php 
	}
?>
								</label>
							</td>
						</tr>
						<tr>
							<th scope="row"><?php _e('Select Header','dkret'); ?></th>
							<td>
								<select style="width: 200px;" name="header_picture" onchange="updateHeader( this )">
									<option value="<?php echo $options['headerimage'] ?>"><?php if ($options['headerimage'] == 'standard') { _e('Standard Header Image','dkret'); } else { echo $options['headerimage']; } ?></option>
									<option value=""> ------ </option>
									<option value="standard"><?php _e('Standard Header Image','dkret'); ?></option>
<?php
	$picture_dir = @ dir(ABSPATH . '/wp-content/themes/' . get_template() . '/images/headers');
	if ($picture_dir)	{
		while(($file = $picture_dir->read()) !== false)	{
			if (!preg_match('|^\.+$|', $file) && !preg_match('|^CVS$|', $file) && preg_match('/\.(gif|jpg|png)$/i',$file))	{
				$picture_files[] = $file;
			}
		}
		if ($picture_dir && $picture_files)	{
			foreach ($picture_files as $picture_file) {
				echo "<option value=\"".$picture_file."\">".$picture_file."</option>";
			}
		}
		else {
			$options['headerimage'] == 'standard';
			update_option('dkret3', $options);
		}
	}
?>
								</select>							
								<br/>
<?php
	global $wpdb;
	$picname = $options['headerimage'];

	if ($picname != 'standard')	{
		$picture_title = $picname;
	}	else {
		$picture_title = __('Standard Header Image','dkret');
	}
	if ($picname != 'standard') {
		$pic = '../wp-content/themes/' . get_template() . '/images/headers/'.$picture_title;
	}	else {
		$pic = '../wp-content/themes/' . get_template() . '/images/bg_header_standard.jpg';
	}
	echo ('<img name="header" src="'.$pic.'" alt="" width="200"></img>');
?>
							</td>
						</tr>
					</tbody>
				</table>

				<table class="form-table">
					<tbody>
						<tr valign="top">
							<th scope="row"><?php _e('Random Header','dkret'); ?></th>
							<td>
								<label for="userandomfeature">
									<input id="userandomfeature" type="checkbox" name="userandomfeature" value="1" <?php checked('1', $options['random_header']) ?> />
									<?php _e('Show Random Header Picture','dkret'); ?>
								</label>
							</td>
						</tr>
						<tr valign="top">
							<th scope="row"><?php _e('Title','dkret'); ?></th>
							<td>
								<label for="show_title">
									<input name="header_text" id="show_title" type="checkbox" value="1" <?php checked('1', $options['blogtitle']) ?> />
									<?php _e('Show Title','dkret') ?>
								</label>
							</td>
						</tr>
						<tr valign="top">
							<th scope="row"><?php _e('RSS-Links','dkret'); ?></th>
							<td>
								<label for="show_rss">
									<input name="header_rss" id="show_rss" type="checkbox" value="1" <?php checked('1', $options['rss']) ?> />
									<?php _e('Show RSS-Links','dkret') ?>
								</label>
							</td>
						</tr>
<?php if (function_exists ('library_url')) { ?>
						<tr valign="top">
							<th scope="row"><?php _e('Now Reading Link','dkret'); ?></th>
							<td>
								<label for="nowreading">
									<input name="nowreading" id="nowreading" type="checkbox" value="1" <?php checked('1', $options['nowreading']) ?> />
									<?php _e('Show Now Reading Link','dkret') ?>
								</label>
							</td>
						</tr>
<?php } ?>
						<tr valign="top">
							<th scope="row"><?php _e('Levels of Navigation','dkret'); ?></th>
							<td>						
								<select style="width: 200px;" name="show_subnavigation" id="show_subnavigation">
									<option value="<?php echo $options['navigationlevel'] ?>">
<?php
	$level = $options['navigationlevel'];
	$messages[0] = __('Hide top navigation', 'dkret');
	$messages[1] = __('Show top-level links only', 'dkret');
	$messages[2] = __('Show first sub-level', 'dkret');
	$messages[3] = __('Show second sub-level', 'dkret');
	$messages[4] = __('Show third sub-level', 'dkret');
	echo $messages[$level]; 
?>
									</option>
									<option value=""> ------ </option>
									<option value="0"><?php echo $messages[0] ?></option>
									<option value="1"><?php echo $messages[1] ?></option>
									<option value="2"><?php echo $messages[2] ?></option>
									<option value="3"><?php echo $messages[3] ?></option>
									<option value="4"><?php echo $messages[4] ?></option>
								</select>
								<br/>
								<?php _e("Choose number of levels you want to show in your top navigation.<br /><b>Add Pages Widget so everybody (even without javascript) can access your site</b>.",'dkret'); ?>
							</td>
						</tr>
					</tbody>
				</table>

				<h3><?php _e('Blog appearance','dkret'); ?></h3>

				<table class="form-table">
					<tbody>
						<tr valign="top">
							<th scope="row"><?php _e('Justification','dkret'); ?></th>
							<td>
								<label for="textalign">
									<input id="textalign" type="checkbox" name="textalign" value="1" <?php checked('1', $options['textalign']) ?> />
									<?php _e('Full justification of the content','dkret'); ?>
								</label>
							</td>
						</tr>
						<tr valign="top">
							<th scope="row"><?php _e('Image borders','dkret'); ?></th>
							<td>
								<label for="imageborders">
									<input id="imageborders" type="checkbox" name="imageborders" value="1" <?php checked('1', $options['imageborders']) ?> />
									<?php _e('Automatic border around every image','dkret'); ?>
								</label>
							</td>
						</tr>
						<tr valign="top">
							<th scope="row"><?php _e('Summary','dkret'); ?></th>
							<td>
								<label for="summary">
									<input id="summary" type="checkbox" name="summary" value="1" <?php checked('1', $options['summary']) ?> />
									<?php _e('Show search, author, category, tags and archive pages as Summary','dkret'); ?>
								</label>
							</td>
						</tr>
					</tbody>
				</table>

				<h3><?php _e('Comments','dkret'); ?></h3>
				<p><?php _e("Dkret uses AJAX to submit comments without reloading the page. Live Commenting is based on 'jQuery AJAX Comments'.",'dkret') ?></p>

				<table class="form-table">
					<tbody>
						<tr valign="top">
							<th scope="row"><?php _e('Live Comments','dkret'); ?></th>
							<td>
								<label for="livecomments">
									<input id="livecomments" type="checkbox" name="livecomments" value="1" <?php checked('1', $options['livecomments']) ?> />
									<?php _e('Use Live Comments','dkret'); ?>
								</label>
							</td>
						</tr>
						<tr valign="top">
							<th scope="row"><?php _e('Preview Comments','dkret'); ?></th>
							<td>
								<label for="previewcomment">
									<input id="previewcomment" type="checkbox" name="previewcomment" value="1" <?php checked('1', $options['previewcomment']) ?> />
									<?php _e('Show Comment Preview','dkret'); ?>
								</label>
							</td>
						</tr>
						<tr valign="top">
							<th scope="row"><?php _e('Comments order','dkret'); ?></th>
							<td>
								<label for="new-comments">
									<input name="commentsorder" id="new-comments" type="radio" value="0" <?php checked('0', $options['commentsorder']); ?> />
									<?php _e('Show new comments first','dkret') ?>
								</label>
								<br/>
								<label for="old-comments">
									<input name="commentsorder" id="old-comments" type="radio" value="1" <?php checked('1', $options['commentsorder']); ?> />
									<?php _e('Show old comments first','dkret') ?>
								</label>
							</td>
						</tr>
					</tbody>
				</table>

				<h3><?php _e('Gallery','dkret'); ?></h3>
				<p><?php _e("Dkret supports the gallery function of Wordpress extensively. Dkret shows galleries if you set a category for your gallery posts.",'dkret') ?></p>

				<table class="form-table">
					<tbody>
						<tr valign="top">
							<th scope="row"><?php _e('Gallery category','dkret'); ?></th>
							<td>
								<label for="textalign">
									<?php wp_dropdown_categories('hide_empty=0&selected='. $options['gallery_cat_id'] .'&show_option_none='. __('No Gallery', 'dkret') ); ?>
								</label>
							</td>
						</tr>
						<tr valign="top">
							<th scope="row"><?php _e('Gallery Link','dkret'); ?></th>
							<td>
								<label for="gallerylink">
									<input id="gallerylink" type="checkbox" name="gallerylink" value="1" <?php checked('1', $options['gallerylink']) ?> />
									<?php _e('Show Gallery in the header navigation','dkret'); ?>
								</label>
							</td>
						</tr>
						<tr valign="top">
							<th scope="row"><?php _e('Gallerie View on blog home','dkret'); ?></th>
							<td>
								<label for="showgallery">
									<input id="showgallery" type="checkbox" name="showgallery" value="1" <?php checked('1', $options['showgallery']) ?> />
									<?php _e('Style gallery categorized posts different','dkret'); ?>
								</label>
							</td>
						</tr>
					</tbody>
				</table>
	
				<p class="submit"><input type="submit" name="Submit" value="<?php _e('Save Changes','dkret') ?> &raquo;" /></p>

			</form>

	</div><!-- .wrap -->

	<div class="wrap">
<?php if (TEMPLATENAME == THEMENAME) { ?>
		<p style="text-align: center;"><?php _e("Help to be had at the <a href='http://diekretzschmars.de/sf-forum/' title='dkret support forum'>dkret support forum</a> or in the <a href='http://diekretzschmars.de/wordpress/themes/dkret-theme/documentation/' title='documentation'>dkret documentation</a>.",'dkret') ?><br />
<?php } else { ?>
		<p style="text-align: center;"><?php printf(__('Help to be had at the <a href="%1$s">%2$s</a> home.<br />You can also always visit the <a href="http://diekretzschmars.de/sf-forum/" title="dkret support forum">dkret support forum</a>.', 'dkret'), TEMPLATEURI, TEMPLATENAME) ?></br>
<?php } ?>
		<?php _e('Credit to the makers of the wonderful <a href="http://www.plaintxt.org/themes/sandbox/">sandbox</a>. Dkret3 would be nothing without it.','dkret') ?></p>
	</div><!-- .wrap -->

<?php } //  function menu())
?>